package com.lyn.solitude_plugin_host.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lyn.solitude_plugin_api.entity.Plugin;
import com.lyn.solitude_plugin_api.plugin.PluginActivityLifecycle;
import com.lyn.solitude_plugin_host.binder.PluginHostBinder;
import com.lyn.solitude_plugin_host.stack.PluginStack;


/**
 * 插件代理activity
 *
 * @author longyn 2024/4/14
 * @version 1.0.0
 */
public class PluginProxyActivity extends AppCompatActivity {

    public static final String PLUGIN_ACTIVITY_CLASS_NAME_KEY = "pluginActivityClassName";
    private PluginActivityLifecycle pluginActivityLifecycle;
    private Resources originalResources;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Plugin plugin = PluginStack.peek();
            String className = getIntent().getStringExtra(PLUGIN_ACTIVITY_CLASS_NAME_KEY);
            Class<?> pluginClass = getClassLoader().loadClass(className);
            pluginActivityLifecycle = (PluginActivityLifecycle) pluginClass.newInstance();
            pluginActivityLifecycle.onPluginHostAidlInterfaceStub(PluginHostBinder.getInstance());
            pluginActivityLifecycle.onPlugin(plugin);
            pluginActivityLifecycle.onActivity(this);
            pluginActivityLifecycle.onOriginalResources(originalResources);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        try {
            pluginActivityLifecycle.onCreate(savedInstanceState);
        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    @Override
    public ClassLoader getClassLoader() {
        Plugin plugin = PluginStack.peek();
        if (plugin != null) {
            return plugin.mate.classLoader;
        }
        return null;
    }

    @Override
    public Resources getResources() {
        originalResources = super.getResources();
        if (pluginActivityLifecycle != null) {
            pluginActivityLifecycle.onOriginalResources(originalResources);
        }
        Plugin plugin = PluginStack.peek();
        if (plugin != null) {
            return plugin.mate.resources;
        }
        return null;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return pluginActivityLifecycle.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, @NonNull Menu menu) {
        return pluginActivityLifecycle.onMenuOpened(featureId, menu);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        pluginActivityLifecycle.onNewIntent(intent);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        pluginActivityLifecycle.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
        pluginActivityLifecycle.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        pluginActivityLifecycle.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        pluginActivityLifecycle.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pluginActivityLifecycle.onDestroy();
        PluginStack.pop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        pluginActivityLifecycle.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pluginActivityLifecycle.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        if (pluginActivityLifecycle.isNotBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
